package com.ruoyi.web.controller.business;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BpmnInfo;
import com.ruoyi.business.service.IBpmnInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 流程定义明细Controller
 *
 * @author ruoyi
 * @date 2022-02-09
 */
@Controller
@RequestMapping("/business/bpmnInfo")
public class BpmnInfoController extends BaseController {
    private String prefix = "business/bpmnInfo";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    @RequiresPermissions("business:info:view")
    @GetMapping("/listPage")
    public String info() {
        return prefix + "/list";
    }

    /**
     * 流程上传页
     */
    @RequiresPermissions("business:bpmnInfo:deployPage")
    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix + "/deployPage";
    }

    /**
     * 查询流程定义明细列表
     */
    @RequiresPermissions("business:info:list")
    @PostMapping("/query")
    @ResponseBody
    public TableDataInfo list(BpmnInfo bpmnInfo) {
        startPage();
        List<BpmnInfo> list = bpmnInfoService.selectBpmnInfoList(bpmnInfo);
        return getDataTable(list);
    }

    /**
     * 导出流程定义明细列表
     */
    @RequiresPermissions("business:info:export")
    @Log(title = "流程定义明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BpmnInfo bpmnInfo) {
        List<BpmnInfo> list = bpmnInfoService.selectBpmnInfoList(bpmnInfo);
        ExcelUtil<BpmnInfo> util = new ExcelUtil<BpmnInfo>(BpmnInfo.class);
        return util.exportExcel(list, "流程定义明细数据");
    }

    /**
     * 新增流程定义明细
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存流程定义明细
     */
    @RequiresPermissions("business:info:add")
    @Log(title = "流程定义明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BpmnInfo bpmnInfo) {
        return toAjax(bpmnInfoService.insertBpmnInfo(bpmnInfo));
    }

    /**
     * 修改流程定义明细
     */
    @RequiresPermissions("business:info:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BpmnInfo bpmnInfo = bpmnInfoService.selectBpmnInfoById(id);
        mmap.put("bpmnInfo", bpmnInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存流程定义明细
     */
    @RequiresPermissions("business:info:edit")
    @Log(title = "流程定义明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BpmnInfo bpmnInfo) {
        return toAjax(bpmnInfoService.updateBpmnInfo(bpmnInfo));
    }

    /**
     * 删除流程定义明细
     */
    @RequiresPermissions("business:info:remove")
    @Log(title = "流程定义明细", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bpmnInfoService.deleteBpmnInfoByIds(ids));
    }
    /**
     * bpmn文件上传和校验
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        String upload = bpmnInfoService.upload(file);
        return AjaxResult.success("data",upload);
    }

    /**
     * 流程信息的提交
     * @param path 上传文件的路径
     * @param bpmnType 流程图的类型
     * @param info 备注信息
     * @return 响应成功信息
     */
    @RequiresPermissions("business:info:deploy")
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String path,String bpmnType,String info) throws IOException {
        bpmnInfoService.deploy(path,bpmnType,info);
        return AjaxResult.success();
    }

    /*流程删除*/
    @RequiresPermissions("business:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id) {
        bpmnInfoService.delete(id);
        return AjaxResult.success();
    }
    /*流程的查看*/
    @RequiresPermissions("business:bpmnInfo:readResource")
    @RequestMapping("/readResource")
    public void readResource(String deploymentId, String type, HttpServletResponse rsp) throws IOException {
        InputStream inputStream = bpmnInfoService.getResoucceByType(deploymentId,type);
        IOUtils.copy(inputStream,rsp.getOutputStream());
    }


}
